<?php

class getProductRelated extends Controller
{

	public function __construct()
	{
		parent::__construct();
		require_once('system/libraries/currency.php');
		$this->obj_currency = new Currency();
	}
	
	public function run()
	{
		$str  = $this->generateProductRelated();
		echo $str;
	}
	
	public function generateProductDetail()
	{
		$objRd		= $this->getProductDetail();
		$colours	= $this->getColour();
		$tags		= $this->getTag();
		$sizes		= $this->getSize();
		
		// get Tags
		if($objRd['tags']==""){
			$tagHTML = "";
		}else{
			$tagIDs = explode(",", $objRd['tags']);
			$tagHTML= "";
			for($i=0;$i<count($tagIDs);$i++){
				$key  	  = $tagIDs[$i];
				$tagHTML .= '<a href="./?tag='.$key.'" title="View more '.$tags[$key]['tag'].'">View more '.$tags[$key]['tag'].'</a> | ';
			}
			$tagHTML = substr($tagHTML, 0, -2);	
			$tagHTML = '<p class="more">'.$tagHTML.'</p>';
		}

		// get Colour
		$colourIDs = explode(",", $objRd['colour']);
		$colourHTML= "";
		for($i=0;$i<count($colourIDs);$i++){
			$key  = $colourIDs[$i];
			$colourHTML .= "<a href='product.html?category=".$objRd['category']."&id=".$objRd['id']."&colour=".$colours[$key]['title']."'><img src=\"".$colours[$key]['img']."\" alt=\"".$colours[$key]['title']."\" /></a>";
		}
		
		// set Images
		$colourPath = "daft_files/catalog/products/".$objRd['skuid']."/";
		if(isset($_GET['colour']) && $_GET['colour']!=""){
			$colourPath .= $_GET['colour']."/";
		}else{
			$colourPath .= $colours[$colourIDs[0]]['title']."/";
		}
		
		$imgsHTML = "";
		for($i=1;$i<=4;$i++){
			$filename = $colourPath.$i.".jpg";
			if(file_exists($filename)) $imgsHTML .= '<li><a href="#" onClick=\'selectedPic("'.$i.'","'.$colourPath.'");return false;\' title=""><img src="image.php?src='.$filename.'&width=93&height=93" alt="'.$objRd['name'].'"/></a></li>';
		}
		
		// get Size
		$sizeIDs = explode(",", $objRd['size']);
		$sizeHTML= "";
		for($i=0;$i<count($sizeIDs);$i++){
			$key  	  = $sizeIDs[$i];
			$sizeHTML .= '<option value="'.$sizes[$key]['size'].'">'.$sizes[$key]['size'].'</option>';
		}

		
		$strHTML = '
<div id="product_detail">
	<div class="gallery">
		'.$tagHTML.'
		<div class="pic">
			<a href="'.$colourPath.'big/1.jpg" class="jqzoom" id="bImg" title="'.$objRd['name'].'"><img src="'.$colourPath.'1.jpg"  id="mImg" title="'.$objRd['name'].'"></a>
		</div>
		<ul class="thumb">
			'.$imgsHTML.'
		</ul>
		<a href="#" title="Tell Friend" class="tell_friend">Tell Friend</a>
		<div class="path"><a href="./" title="">HOME</a> &nbsp;&nbsp;&gt;&nbsp;&nbsp; <a href="./?category='.$objRd['category'].'" title="'.$objRd['categoryName'].'">'.$objRd['categoryName'].'</a> &nbsp;&nbsp;&gt;&nbsp;&nbsp; '.$objRd['name'].'</div>
	</div>
	
	<div class="property">
		<h2>'.$objRd['name'].'</h2>
		<span class="order">'.$objRd['skuid'].'</span>
		<span class="price">'.$this->obj_currency->change_order($objRd['price']).'</span>
		'.$objRd['description'].'
		<div class="product_extra">
			<div id="tool">
				<a href="#" title="" id="slip">SLIP IN WISHLIST</a> <a href="#" title="" id="share_this_product">SHARE THIS PRODUCT</a>
			</div>
			<p class="select_color"><strong>SELECT COLOR</strong>: 
			'.$colourHTML.'
			</p>
			<p class="skin">
				<label><strong>SIZE</strong>: <select id="size" name="size">
						<option value="" selected="selected">- SELECT SIZE -</option>
						'.$sizeHTML.'
					</select>
				</label>
				<label><strong>QUANTITY</strong>: <select id="quantity" name="quantity">
						<option value="" selected="selected">- 1</option>
						<option value="">2</option>
						<option value="">3</option>
						<option value="">4</option>
						<option value="">5</option>
					</select>
				</label>
			</p>
			<p class="size_chart"><img src="images/icon_size_chart.gif" alt="" /> <a href="" title="">size chart</a></p>
			<p class="wrapping"><input type="checkbox" name="gift_wrapping" id="" /> I want <a title="" class="wrapping_link">'.$objRd['gwTitle'].'</a> ('.$this->obj_currency->change_order($objRd['gwPrice']).')</p>
			<p class="submits"><a href="#" title="ADD TO CART" class="addcart">ADD TO CART</a><a href="#" title="CHECKOUT" class="checkout">CHECKOUT</a></p>

			<div id="gift_wrapping" class="none">
				<span class="icon_tip"></span>
				<img src="'.$objRd['gwImg'].'" alt="'.$objRd['gwTitle'].'" />
			</div> <!-- gift_wrapping end -->
			<div id="share_product" class="none">
				<div class="cons">
					<span class="icon_tip"></span>
					<ul>
						<li><a href="#" title="Facebook"><img src="images/sociable/share_facebook.gif" alt="Facebook" /></a></li>
						<li><a href="#" title="Twitter"><img src="images/sociable/share_twitter.gif" alt="Twitter" /></a></li>
						<li><a href="#" title="Delicious"><img src="images/sociable/share_delicious.gif" alt="Delicious" /></a></li>
						<li><a href="#" title="Digg"><img src="images/sociable/share_digg.gif" alt="Digg" /></a></li>
						<li><a href="#" title="E-mail"><img src="images/sociable/icon_email.gif" alt="E-mail" /></a></li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>		
		';

		return $strHTML;
	}
	
	public function getProductDetail()
	{
		$id    = $_GET['id'];
		$this->import('Database');
		$sql   = 'SELECT product_catalog.*,tl_taxonomy.name as categoryName,catalog_wrapping.title as gwTitle,catalog_wrapping.img as gwImg,catalog_wrapping.price as gwPrice FROM `product_catalog`,`tl_taxonomy`,`catalog_wrapping` where product_catalog.id='.$id.' and product_catalog.category=tl_taxonomy.id and product_catalog.wrapping=catalog_wrapping.id';
		$objRd = $this->Database->execute($sql); 
		$objRd->next();
		
		$rdArray['id'] 			= $objRd->id;
		$rdArray['pid'] 		= $objRd->pid;
		$rdArray['category'] 	= $objRd->category;
		$rdArray['categoryName']= $objRd->categoryName;
		$rdArray['name'] 		= $objRd->name;
		$rdArray['description'] = $objRd->description;
		$rdArray['price'] 		= $objRd->price;
		$rdArray['skuid'] 		= $objRd->skuid;
		$rdArray['colour'] 		= $objRd->colour;
		$rdArray['size'] 		= $objRd->size;
		$rdArray['related_skuid'] = $objRd->related_skuid;
		$rdArray['gwTitle'] 	= $objRd->gwTitle;
		$rdArray['gwImg'] 		= $objRd->gwImg;
		$rdArray['gwPrice'] 	= $objRd->gwPrice;
		$rdArray['tags'] 		= $objRd->tags;
		
		return $rdArray;		
	}
	
	public function getColour()
	{
		$dataArray = array();
		$this->import('Database');
		$objRd = $this->Database->execute("SELECT * FROM `catalog_colour` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['title'] = $objRd->title;
			$dataArray[$objRd->id]['img'] 	= $objRd->img;
		}
		return $dataArray;
	}
	
	public function getSize()
	{
		$dataArray = array();
		$this->import('Database');
		$objRd = $this->Database->execute("SELECT * FROM `catalog_size` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['size'] = $objRd->size;
		}
		return $dataArray;
	}	

	public function getTag()
	{
		$dataArray = array();
		$this->import('Database');
		$objRd = $this->Database->execute("SELECT * FROM `catalog_tags` Order by id asc");
		
		while ($objRd->next())
		{
			$dataArray[$objRd->id]['tag'] = $objRd->tag;
		}
		return $dataArray;
	}
	
}

$objGetRd = new getProductDetail();
$objGetRd->run();

?>